package com.wtgroup.demo.mianshi.线程.d03_aqs;

import com.wtgroup.demo.common.util.SleepUtil;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/3/8 14:59
 */
public class D {

    static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();

        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                // for (int i=0; i < 10000; i++) {
                //     count++;
                // }
                count++;
                SleepUtil.sleep(1000); // 占着
            } finally {
                lock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {
            SleepUtil.sleep(1);
            lock.lock();
            try {
                // for (int i=0; i < 10000; i++) {
                //     count++;
                // }

                count++;
            } finally {
                lock.unlock();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count = " + count);

        System.out.println("!!!!!!!!!!!!!!!!!!!");
    }
}
